package com.aliyun.oss.service.impl;

import com.aliyun.oss.pojo.ArticleContent;
import com.aliyun.oss.pojo.ArticleInfo;
import com.aliyun.oss.pojo.ArticleListParams;
import com.aliyun.oss.mapper.ArticleManage;
import com.aliyun.oss.service.ArticleManageService;
import com.aliyun.oss.utils.AliyunOSSOperator;
import com.aliyun.oss.utils.JwtUtil;
import com.aliyuncs.exceptions.ClientException;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ArticleManageServiceImpl implements ArticleManageService {
    @Autowired
    ArticleManage articleManage;
    @Autowired
    AliyunOSSOperator aliyunOSSOperator;
    @Override
    public List<ArticleInfo> getArticleList(String authHeader, ArticleListParams articleListParams) {
        Claims claims = JwtUtil.analysisToken(authHeader);
        Integer userId = (Integer) claims.get("id");
        Integer  pageBegin = (articleListParams.getPageNum() - 1) * (articleListParams.getPageSize());
        List<ArticleInfo> articleInfos = articleManage.getArticleList(userId,pageBegin,articleListParams);
        return articleInfos;
    }

    @Override
    public Integer getAllArticleList(String authHeader) {
        Claims claims = JwtUtil.analysisToken(authHeader);
        Integer userId = (Integer) claims.get("id");
        Integer total = articleManage.getAllArticleList(userId);
        return total;
    }

    @Override
    public void addArticle(String authHeader, ArticleContent articleContent) {
        Claims claims = JwtUtil.analysisToken(authHeader);
        Integer userId = (Integer) claims.get("id");
        articleContent.setUserId(userId);
//        创建当前时间给articleContent
        articleContent.setPubDate(LocalDateTime.now());
        articleManage.addArticle(articleContent);
    }

//    查询文章
    @Override
    public ArticleContent getArticleInfo(String authHeader, Integer id) {
        Claims claims=JwtUtil.analysisToken(authHeader);
        Integer userId = (Integer) claims.get("id");
        ArticleContent articleContent = articleManage.getArticleInfo(userId,id);
        return articleContent;
    }

    @Override
    public void updateArticleInfo(String authHeader, ArticleContent articleContent) throws IOException, ClientException {
        Claims claims = JwtUtil.analysisToken(authHeader);
        Integer userId = (Integer) claims.get("id");
        articleContent.setUserId(userId);
//        处理图片
        MultipartFile coverImg=articleContent.getCoverImg();
        if (coverImg == null || coverImg.isEmpty()) {
            articleManage.updateArticleInfo(articleContent);
        }
        else {
            String originalFilename = coverImg.getOriginalFilename();
            byte[] content = coverImg.getBytes();
            String upload = aliyunOSSOperator.upload(content, originalFilename);
            articleContent.setImgUrl(upload);
            articleManage.updateArticleInfo(articleContent);
        }
    }
}
